package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 *角色控制层
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;

    //查询所有角色信息
    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.QUERY_ROLE_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    //根据id查询指定的角色信息
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询角色信息成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色信息失败");
        }
    }

    //查询role关联的权限id集合
    @RequestMapping("/findPermissionIds")
    public Result findPermissionIds(Integer id) {
        try {
            List<Integer> list = roleService.findPermissionIds(id);
            return new Result(true, "查询PermissionIds成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询PermissionIds失败");
        }
    }

    //查询role关联的菜单id集合
    @RequestMapping("/findMenuIds")
    public Result findMenuIds(Integer id) {
        try {
            List<Integer> menuIds = roleService.findMenuIds(id);
            return new Result(true, "查询menuIds成功", menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询menuIds失败");
        }
    }

    //添加角色
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role) {
        try {
            roleService.add(permissionIds, menuIds, role);
            return new Result(true, "角色添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色添加失败");
        }
    }

    //角色编辑
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds, Integer[] menuIds, @RequestBody Role role) {
        try {
            roleService.edit(permissionIds, menuIds, role);
            return new Result(true, "角色编辑成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色编辑失败");
        }
    }

    //删除指定角色
    @RequestMapping("/delById")
    public Result delById(Integer id) {
        try {
            roleService.delById(id);
            return new Result(true, "删除成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }
}